<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['category']) && (($_SESSION['category']&1<<1) == 0))) {
	$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
$intType			= $objRequest->getParam('ptype', 0);
$strName			= $objRequest->getParam('pname', '');
$strSlug			= $objRequest->getParam('pslug', '');
$strTag				= $objRequest->getParam('ptag', '');
$strShort			= $objRequest->getParam('pshort', '');
$oIcon				= $_FILES['pimage'];
$oIconAct			= $_FILES['pimage2'];
$intIseq			= $objRequest->getParam('piseq', 0);
$intParentId		= $objRequest->getParam('pparentid', 0);
$intAdv				= $objRequest->getParam('padv', 0);
$arrErr				= null;
$strIcon			= '';
$strIconAct			= '';
$strName			= Ecore_Function::stripTags($strName,-2);
$strShort			= Ecore_Function::stripTags($strShort,-2);
if($strName == '') $arrErr['name_required'] = $arrLocale['name_required'];
elseif($strName != '' && strlen($strName) < VL_NAME_MINLENGTH) $arrErr['name_minlength'] = $arrLocale['name_minlength'];
if($strShort != '' && strlen($strShort) > VL_SHORT_MAXLENGTH) $arrErr['short_maxlength'] = $arrLocale['short_maxlength'];
if(!isset($arrErr) && isset($oIcon)) {
	$arrIcon	= Ecore_Upload_Category::upload($oIcon);
	if($arrIcon['error'] == 0) {
		$strIcon	= $arrIcon['name'];
	}else {
		$arrErr['upload']	= $arrError['upload'];
	}
}
if(!isset($arrErr) && isset($oIconAct)) {
	$arrIconAct	= Ecore_Upload_Category::upload($oIconAct);
	if($arrIconAct['error'] == 0) {
		$strIconAct	= $arrIconAct['name'];
	}else {
		$arrErr['upload']	= $arrErr['upload'];
	}
}

if(!isset($arrErr)) {
	if(isset($_SESSION['CATEGORY_POST'])) unset($_SESSION['CATEGORY_POST']);
	if(isset($_SESSION['CATEGORY_ERROR'])) unset($_SESSION['CATEGORY_ERROR']);	
	$strSlug		= $strSlug == '' ? Ecore_Rewrite_Common::slug($strName) : $strSlug;
	$intStoreId		= isset($_SESSION['MAIN_STORE']) ? $_SESSION['MAIN_STORE'] : 0;
	$objCategory 	= new Category();
	$intError		= $objCategory->add($AppUI->id, $intStoreId, $strName, $strSlug, $strShort, 
										$strIcon, $strIconAct, $intParentId, $intIseq, $intType,
										$strTag, $intAdv);
	if($intError == 0) {
		Ecore_Function::setMsg($arrMessage['category_add']);
		$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
	}
}
if($intError > 0) {
	$_SESSION['CATEGORY_POST']		= $objRequest->getParams();
	$_SESSION['CATEGORY_ERROR']		= $arrErr;
	$this->_redirect(HOST_BACKEND . 'category/add?parentid=' . $intParentId);
}